//
//  SoundPlayer.m
//  CoreGraphicsTest
//
//  Created by aewatson on 12/11/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SoundPlayer.h"


@implementation SoundPlayer

@synthesize shotPlayer;
@synthesize explosionPlayer;
@synthesize blockPlayer;
@synthesize roundEndPlayer;

-(id) init {
    self = [super init];
    
    // load files
    [self setShotPlayer:[self loadSoundWithFileName:@"shot.wav"]];
    [self setExplosionPlayer:[self loadSoundWithFileName:@"explosion.wav"]];
    [self setBlockPlayer:[self loadSoundWithFileName:@"block.wav"]];
    [self setRoundEndPlayer:[self loadSoundWithFileName:@"roundEnd.wav"]];

    return self;
}

-(AVAudioPlayer*) loadSoundWithFileName:(NSString*) fileName {
    // load files
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], fileName]];
    
	NSError *error;	AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
	audioPlayer.numberOfLoops = 0;
    if (audioPlayer == nil)
		NSLog([error description]);
    return audioPlayer;
}

- (void) playShot {
    if([shotPlayer isPlaying]) {
        [shotPlayer setCurrentTime:0];
    } else {
        [[self shotPlayer] play];
    }
}

- (void) playExplosion {
    if([explosionPlayer isPlaying])
    {
        [explosionPlayer setCurrentTime:0];
    } else {
        [[self explosionPlayer] play];
    }
}

- (void) playRoundEnd {
    if([roundEndPlayer isPlaying])
    {
        [roundEndPlayer setCurrentTime:0];
    } else {
        [[self roundEndPlayer] play];
    }
}

- (void) playBlock {
    if([blockPlayer isPlaying]) {
        [blockPlayer setCurrentTime:0];
    }
    else {
        [[self blockPlayer] play];
    }
}

- (void)dealloc {
	[shotPlayer release];
    [explosionPlayer release];
    [blockPlayer release];
    [roundEndPlayer release];
	[super dealloc];
}

@end
